<template>
  <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
    v-model="open" v-if="open" :title="title" @close="dialogClose" class="dialog1" width="1880" :fullscreen="true">
    <el-tabs v-model="activityName" class="demo-tabs" @tab-change="tabChange">
      <el-tab-pane v-for="(item, index) in tabsView1" :label="item.tableName" :name="item.tFId" :key="index">
        <template v-if="item.modelType == 3">
          <zbtb ref="zbtbRef" v-if="activityName == item.tFId" :params="item" :myId="myId" :userStepId="userStepId"
            :userStatus="userStatus" :nextUser="nextUser" :tableId="item.tableId" :methodId="methodId" :pId="pId" />
        </template>

        <template v-else-if="item.modelType == 7 || item.isPrimary == 2">
          <labLine ref="lineRef" v-if="activityName == item.tFId" :params="item" :myId="myId" :userStepId="userStepId"
            :userStatus="userStatus" :tableId="item.tableId" :methodId="methodId" />
        </template>

        <template v-else>
          <pxyb ref="txybRef" v-if="activityName == item.tFId" :params="item" :myId="myId" />
        </template>
      </el-tab-pane>
    </el-tabs>

    <template #footer>
      <div class="dialog-footer">
        <el-row style="justify-content: end">
          <el-dropdown split-button type="success" @click="submitData(1)" style="margin: 0 10px;"
            v-if="userStatus == 0 && userStepId !== 2 && userStepId != 4">
            提交
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="submitData(1)">
                  <el-button type="success" link>随机审核</el-button>
                </el-dropdown-item>
                <el-dropdown-item @click="submitData(2)">
                  <el-button type="success" link>手动审核</el-button>
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
          <el-button type="success" v-if="userStatus == 0 && (userStepId == 2 || userStepId == 4)"
            @click="submitData(1)">完成</el-button>
          <el-button type="success" v-if="isException || (userStatus == 0 && userStepId != 0)"
            @click="returnWrite">退回给填报员</el-button>
            <el-button type="success" @click="handleArchiveTable" v-if="isException && userStatus == 1 ">
                重载数据
            </el-button>
          <el-button type="success" @click="showRecord">流程记录</el-button>
          <el-button type="success" @click="printDateForm">打印</el-button>
          <el-button type="danger" @click="handleClear(false)" v-if="userStatus != 0">关 闭</el-button>
          <el-button type="danger" @click="handleClear(false)" v-else>取 消</el-button>
        </el-row>
      </div>
    </template>
    <!-- 流程记录 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable v-model="recordOpen"
      :title="recordTitle" width="950">
      <el-table :data="recordList" v-loading="loadingRecord" max-height="60vh">
        <el-table-column label="步骤" align="center">

          <template #default="scope">
            <span v-if="scope.row.userStepId == 0">原始记录填报</span>
            <span v-if="scope.row.userStepId == 1">复核</span>
            <span v-if="scope.row.userStepId == 2">审核</span>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="nickName" align="center" />
        <el-table-column label="开始时间" prop="createTime" align="center" />
        <el-table-column label="结束时间" prop="finishTime" />
        <el-table-column label="状态">

          <template #default="scope">
            <span v-if="scope.row.userStatus == 0">未完成</span>
            <span v-if="scope.row.userStatus == 1">已完成</span>
            <span v-if="scope.row.userStatus == 2">退回</span>
          </template>
        </el-table-column>
      </el-table>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="danger" @click="recordOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </el-dialog>
</template>

<script setup>
import zbtb from "../labMethod/components/zbtb.vue";
import pxyb from "../labMethod/components/pxyb.vue";
import labLine from "../labMethod/components/line.vue";
import { myTaskFormList, getSelectView } from "@/api/lab/labMethod";
import bus from "@/utils/eventBus";
import {
  userRecordList,
  reloadArchiveFile
} from "@/api/myTask/myTask";
const { proxy } = getCurrentInstance();

import { popScopeId } from "vue";
const methodId = ref("");
const pId = ref();
const tabsView1 = ref([]);
const activityName = ref("");
const title = ref("");
const open = ref(false);
const userStatus = ref(0);
const userStepId = ref(0);
const nextUser = ref({});
const myId = ref("");
const formula = ref("");
const formulaList = ref([]);
const showFormulaBtn = ref(false);
const recordOpen = ref(false);
const loadingRecord = ref(false);
const recordList = ref([]);
const recordTitle = ref("流程记录");
const isException = ref(false);//是否是异常数据,异常数据三审后也可能退回
function handleCheck(id, exception) {
  if (exception) {
    isException.value = exception;
  }
  open.value = true;
  title.value = "原始记录";
  myId.value = id;
  myTaskFormList(id).then((res) => {
    if(!res.data){
      return;
    }
    userStatus.value = res.data.userStatus;
    userStepId.value = res.data.userStepId;
    if (res.data.nextUserId) {
      nextUser.value.nextUserId = res.data.nextUserId;
      nextUser.value.nextUserName = res.data.nextNickName;
    }
    title.value = "原始记录" + (userStepId.value == 1 ? "复核" : userStepId.value == 2 ? "审核" : "");
    if (!pId.value) {
      getSelectView(res.data.taskId).then(res1 => {
        pId.value = res1.data.pId;
        tabsView1.value = res.data.myTaskFormList;
        nextTick(() => {
          activityName.value = tabsView1.value[0].tFId;
        });
      });
    } else {
      tabsView1.value = res.data.myTaskFormList;
      nextTick(() => {
        activityName.value = tabsView1.value[0].tFId;
      });
    }
  });
}


bus.off("closeDialog");
bus.on("closeDialog", () => {
  handleClear(true);
})
function dialogClose(params) {
  handleClear(true);
}

function printDateForm() {
  bus.emit("printDateForm");
}

function tabChange(params) { }

function submitData(bs) {
  let params = { bs: bs, myId: myId.value }
  // if (userStepId.value == 2) {//2:说明当前是最后环节。
  //   params.isEnd = true;
  // }
  bus.emit("submitData" + activityName.value, params);
}


function returnWrite() {
  bus.emit("returnWrite", isException.value);
}

function showRecord() {
  recordOpen.value = true;
  loadingRecord.value = true;
  userRecordList(myId.value).then((res) => {
    recordList.value = res.data;
    loadingRecord.value = false;
  });
}
watch(
  () => open.value,
  () => {
    if (open.value) {
      bus.off("deleteTabView");
      bus.on("deleteTabView", (tFId) => {
        tabsView();
      })
    }
  }
);

function handleArchiveTable(){
  reloadArchiveFile(myId.value).then((res) => {
    proxy.$modal.msgSuccess("重载成功！");
    handleCheck(myId.value,isException.value)
  });
}

const emit = defineEmits(["close"])

function handleClear(isSeach) {
  tabsView1.value = [];
  formulaList.value = [];
  methodId.value = "";
  open.value = false;
  emit("close", isSeach);
  bus.off("getFormulaList");
  bus.off("handleReloadTable" + activityName.value);
  pId.value = ""
}
defineExpose({
  handleCheck,
  methodId,
  pId,
});
</script>